<template>
  <div>
    <div style="margin-bottom: 20px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }" style="margin-left: 20px">系统管理</el-breadcrumb-item>
        <el-breadcrumb-item><a href="/">角色管理</a></el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入名称..." suffix-icon="el-icon-search" class="ml-5"
                v-model="name"></el-input>
      <el-button class="ml-5" type="primary" @click="seachKehu">搜索</el-button>
      <el-button @click="resetForm">重置</el-button>
    </div>
    <div style="margin: 10px 0">
      <el-button type="primary" @click="openKehudialog">新增<i class="el-icon-circle-plus-outline" style="margin-left: 2px"></i></el-button>
      <el-button type="danger" @click="delbath">批量删除<i class="el-icon-remove-outline" style="margin-left: 2px"></i></el-button>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="序号" width="50" fixed></el-table-column>
      <el-table-column prop="name" label="名称" >
      </el-table-column>
      <el-table-column prop="description" label="描述" >
      </el-table-column>
      <el-table-column label="操作"  fixed="right">
        <template slot-scope="scope">
          <el-button type="info" slot="reference" @click="selectMenu(scope.row.id)"><i class="el-icon-remove-menu"></i>分配菜单</el-button>
          <el-button type="primary"  @click="updatekehu(scope.row)"><i class="el-icon-edit"></i>编辑</el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="确定要删除这条记录吗？"
              @confirm="deletekehu(scope.row.id)"
          >
            <el-button type="danger" slot="reference"><i class="el-icon-remove-outline"></i>删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10,15,20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>


    <el-dialog title="权限信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-position="left">
        <el-form-item label="名称" label-width="100px">
          <el-col :span="16">
            <el-input v-model="form.name" autocomplete="off" placeholder="请输入名称"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="描述" label-width="100px">
          <el-col :span="16">
            <el-input v-model="form.description" autocomplete="off" placeholder="请输入描述" type="textarea"
                      :autosize="{ minRows: 2, maxRows: 4}"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveKehu">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="菜单信息" :visible.sync="dialogFormVisible2" width="30%" :before-close="handleClose">
      <el-form :model="form" label-position="left">
        <el-tree
            :data="data"
            :props="props"
            show-checkbox
            node-key="id"
            ref="tree"
            :default-expanded-keys="expanded"
            :default-checked-keys="checked"
            @check-change="handleCheckChange">
          <span class="custom-tree-node" slot-scope="{ node, data }">
            <span><i :class="data.icon"></i>{{ data.name }}</span>
        </span>
        </el-tree>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="noSave">取 消</el-button>
        <el-button type="primary" @click="saveRoleMenu">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>


<script>
import request from "@/utils/request";
export default {
  name: "Role",
  data(){
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      name: "",
      form:{
        id:"",
        name:"",
        description:""
      },
      dialogFormVisible: false,
      dialogFormVisible2: false,
      imageUrl: '',
      headerBg: 'headerBg',
      multipleSelection: [],
      data: [],
      props: {
        label: 'name'
      },
      checked:[],
      expanded:[],
      roleId:0
    }
  },
  created() {
    this.load()
  },
  methods: {
    handleClose(done){
      done();
      this.noSave()
    },
    noSave(){
      this.dialogFormVisible2 = false
      this.checked=[]
    },
    saveRoleMenu(){
      this.request.post("role/roleMenu/"+this.roleId,this.$refs.tree.getCheckedKeys()).then(res=>{
        if (res.code=="200"){
          this.dialogFormVisible2=false
          this.checked=[]
          this.$message.success("分配成功")
        }
      })
      console.log(this.$refs.tree.getCheckedKeys());
    },
    handleCheckChange(data) {
      this.checked.push(data.id);
      console.log("-----------------------------------",this.checked);
    },
    selectMenu(roleId){
      this.roleId=roleId

      request.get("menu/all", {
        params: {
        }
      }).then(res => {
        if (res.code=="200"){
          this.data = res.data
          this.expanded=this.data.map(value => value.id)
        }
  request.get("role/roleMenu/"+roleId).then(res=>{
    if (res.code=="200"){
      this.checked=res.data
    }
  })
        this.dialogFormVisible2=true
      })
    },
    delbath(){
      let ids=this.multipleSelection.map(v =>v.id)
      request.post("role/deletebatch",ids).then(res => {
        if (res.code == "200") {
          this.load()
          this.$message.success("删除客户成功")
        } else {
          this.$message.error("删除客户失败")
        }
      })
    },
    deletekehu(id) {
      console.log(id)
      request.post("role/delete",id).then(res => {
        if (res.code == "200") {
          this.load()
          this.$message.success("删除客户成功")
        } else {
          this.$message.error("删除客户失败")
        }
      })
    },
    updatekehu(row){
      this.form=row
      this.dialogFormVisible=true
    },
    saveKehu(){
      this.dialogFormVisible = false
      if (this.form.id){
        request.post("role/update",this.form).then(res =>{
          if (res.code=="200"){
            this.load()
            this.$message.success("修改客户信息成功")
          }else {
            this.$message.error("修改客户信息失败")
          }
        })

      }else {
        request.post("role",this.form).then(res =>{
          if (res.code=="200"){
            this.load()
            this.$message.success("新增客户成功")
          }else {
            this.$message.error("新增客户失败")
          }
        })
      }
    },
    openKehudialog(){
      this.form={}
      this.dialogFormVisible=true
    },
    resetForm() {
      this.name = ""
      this.load()
    },
    seachKehu() {
      this.load()
    },
    handleCurrentChange(data) {
      this.pageNum = data
      this.load()
    },
    handleSizeChange(data) {
      this.pageSize = data
      this.load()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    load() {
      request.get("role/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name
        }
      }).then(res => {
        if (res.code=="200"){
          this.tableData = res.data.records
          this.total = res.data.total
        }

      })
    }
  }

}
</script>
<style scoped>

</style>